
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>
    
  koa-cache-control 中间件使用详解 - LZH-DEV-TECH-SHARE
  

  </title>
  <meta name="author" content="">
  <meta name="description" content="">

  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <link href="asset/css/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="atom.xml" rel="alternate" title="LZH-DEV-TECH-SHARE" type="application/atom+xml">
  <script src="asset/js/modernizr-2.0.js"></script>
  <script src="asset/js/jquery.min.js"></script>
  <script src="asset/highlightjs/highlight.pack.js"></script>
  <link href="asset/highlightjs/styles/solarized_light.css" media="screen, projection" rel="stylesheet" type="text/css">
  <script>hljs.initHighlightingOnLoad();</script>

  <style type="text/css">
  .cat-children-p{ padding: 6px 0px;}
  .hljs{background: none;}
  </style>
  <script type="text/javascript">
  var isAddSildbar = true;
  </script>
  <script src="asset/js/octopress.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
//链接新开窗口
function addBlankTargetForLinks () {
  $('a[href^="http"]').each(function(){
      $(this).attr('target', '_blank');
  });
}
$(document).ready(function(event) {
  addBlankTargetForLinks();
});
</script>
<body   >
  <header role="banner"><hgroup>
  <h1><a href="index.html">LZH-DEV-TECH-SHARE</a></h1>
  
    <h2></h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="http://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">

  <li id=""><a target="self" href="index.html">Home</a></li>

  <li id=""><a target="_self" href="archives.html">Archives</a></li>

</ul>

</nav>
  <div id="main">
    <div id="content"> 
<div>
	<article class="hentry" role="article">
	<header>
			  	<h1 class="entry-title">koa-cache-control 中间件使用详解</h1>
				<p class="meta"><time datetime="2018-10-09T19:33:31+08:00" pubdate data-updated="true">2018/10/9</time></p>
			 </header>
		  	<div class="entry-content">
			  	<ul>
<li>
<a href="#toc_0">什么是koa-cache-control</a>
</li>
<li>
<a href="#toc_1">为什么使用koa-cache-control</a>
</li>
<li>
<a href="#toc_2">如何安装</a>
</li>
<li>
<a href="#toc_3">使用示例</a>
</li>
<li>
<a href="#toc_4">现实demo</a>
</li>
</ul>


<h2 id="toc_0">什么是koa-cache-control</h2>

<blockquote>
<p>A simple method for managing cache control headers from your application. It also tries to provide a simple set of rules for common use cases such as setting &#39;max-age=0&#39; when &#39;no-cache&#39; is present by default.<br/>
中文: 提供简单的方式来管理 cache control 头信息, 也提供了几个简单的配置规则, 例如 当指定 &#39;no-cache&#39; 的时候设置&#39;max-age=0&#39;.</p>
</blockquote>

<h2 id="toc_1">为什么使用koa-cache-control</h2>

<p>要了解为什么要使用cache-control我们要先了解什么是 cache<br/>
具体可以参考: </p>

<ul>
<li><a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=zh-cn">https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=zh-cn</a></li>
<li><a href="https://segmentfault.com/a/1190000005704489?utm_source=caibaojian.com">https://segmentfault.com/a/1190000005704489?utm_source=caibaojian.com</a></li>
</ul>

<p>接下来我们说为什么koa-cache-control中间件, 原因很简单, 因为这样更简单, 更高效, 更方便.</p>

<h2 id="toc_2">如何安装</h2>

<p><code>$ npm install koa-cache-control</code></p>

<h2 id="toc_3">使用示例</h2>

<pre><code class="language-javascript">const Koa = require(&#39;koa&#39;)
const morgan = require(&#39;koa-morgan&#39;)
const path = require(&#39;path&#39;);
const static = require(&#39;koa-static&#39;)
const fs = require(&#39;fs&#39;)
var cacheControl = require(&#39;koa-cache-control&#39;);

const app = new Koa();

app.use(cacheControl({
  maxAge: 86400
}));

// logger
app.use(morgan(&#39;:remote-addr - :remote-user [:date[clf]] &quot;:method :url HTTP/:http-version&quot; :status :res[content-length] :response-time ms&#39;));

// static assets
app.use(static(path.resolve(__dirname, &#39;./dist&#39;)));

//异步读取文件的形式
app.use(async (ctx, next) =&gt; {
  ctx.type = &#39;html&#39;;
  ctx.body = await fs.createReadStream(path.resolve(__dirname, &#39;./dist/index.html&#39;));
});

module.exports = app;
</code></pre>

<h2 id="toc_4">现实demo</h2>

<p>我们线上的知识库测试服务器解决缓存加载问题, 的就是 koa-cache-control 来支持的<br/>
访问地址: <a href="http://120.79.79.6:9000/">http://120.79.79.6:9000/</a></p>

			</div>

		
	  
		<footer>
		 <p class="meta">

			<strong>Categories:</strong>&nbsp; 
			<span class="categories">
			
			    <a class='category' href='front-end.html'>front-end</a>&nbsp;
			 
			</span>
		    </p>
		    <p class="meta">
		      
		 </p>
	    
		<div class="sharing">
		  
          

          

		</div>

	    <p class="meta">
	    
	        <a class="basic-alignment left" href="15391742760274.html" 
	        title="Previous Post: XRebel 使用详解">&laquo; XRebel 使用详解</a>
	    
	    
	        <a class="basic-alignment right" href="15390839723979.html" 
	        title="Next Post: koa-static 中间件使用详解">koa-static 中间件使用详解 &raquo;</a>
	    
	    </p>
	  </footer>
	</article>
</div>
 <aside class="sidebar"> 

	<section>
	  <h1>Categories</h1>
	  <ul id="recent_posts">
	  
	      <li class="post">
	        <a href="version-control.html"><strong>version-control&nbsp;(2)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="shell.html"><strong>shell&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="back-end.html"><strong>back-end&nbsp;(4)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="front-end.html"><strong>front-end&nbsp;(5)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="version-control-1.html"><strong>version-control&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="other.html"><strong>other&nbsp;(2)</strong></a>
	        
	        
	        
	      </li>
	   
	  </ul>
	</section>
	<section>
	  <h1>Recent Posts</h1>
	  <ul id="recent_posts">
	  
	      
		      <li class="post">
		        <a href="15413898294420.html">RESTful API 最佳实践</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15411425055347.html">LZH-DEV-GROUP Git Workflow 实践详解</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15410718240579.html">如何使用PM2进程管理器, 守护java进程</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15408811446857.html">一篇文章搞定 centos7 上的 mysql 安装</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="15408703788547.html">一篇文章搞定 centos7 下安装 nvm和node</a>
		      </li>
	     
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	   
	  </ul>
	</section>
	
</aside> </div></div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2014 -  -
  <span class="credit">Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a> &nbsp;&nbsp; Theme by <a href="http://octopress.org">Octopress</a></span>
</p>

</footer>

  
    




</body>
</html>